package projects.android.sms.object;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;

/**
 * Helper class for resolving names for certain phone numbers.
 * 
 * @author Thomas Jaeger
 * @author Mario Kraml
 * @author Tassilo Posegga
 * @author Robert Schenkenfelder
 * 
 */
public class NumberHelper {

	/**
	 * @param phoneNumber
	 * @return the name resolved to the phoneNumber in Contacts
	 */
	public static String resolveNumberToName(Context context, String phoneNumber) {
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

		Cursor c = context.getContentResolver().query(uri, new String[] { Contacts.DISPLAY_NAME }, null, null, null);
		try {
			c.moveToFirst();
			String displayName = c.getString(0);
			return displayName;
		} catch (Exception e) {
			e.printStackTrace();
			return phoneNumber;
		} finally {
			c.close();
		}
	}
}
